/*******************************************************************************
 * Copyright (c) 2012 March and Conquest.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package mrusanov.towers.main.ui;

import java.awt.GridLayout;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.JPanel;

import com.google.common.collect.Maps;

import mrusanov.towers.players.Player;
import mrusanov.towers.resources.Resource;

public class ResourcePanel extends JPanel {
	private static final long serialVersionUID = -1239516257223065855L;

	private final Map<Resource, ResourceComponent> resourceComponents;

	public ResourcePanel() {
		resourceComponents = Maps.newEnumMap(Resource.class);
		setLayout(new GridLayout(3, 1));

		for (Resource resource : Resource.values()) {
			ResourceComponent component = new ResourceComponent(resource);
			resourceComponents.put(resource, component);
			add(component);
		}
	}

	public void refreshResources(Player player) {
		for (Entry<Resource, ResourceComponent> entry : resourceComponents.entrySet()) {
			ResourceComponent component = entry.getValue();
			Resource resource = entry.getKey();
			component.setIncome(player.getIncome().getResource(resource));
			component.setCount(player.getResources().getResource(resource));
		}
	}
}
